#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
host_os=`uname -s | tr "[:upper:]" "[:lower:]"`
NDK_DEBUG=1
SRCDIR=${DIR}/luajit-2.1
NDK=${ANDROID_NDK_ROOT}

APP_ANDROID_ROOT=${DIR}/android

cd "$SRCDIR"

# Android/ARM, armeabi (ARMv5TE soft-float), Android 2.2+ (Froyo)
DESTDIR=$APP_ANDROID_ROOT/prebuilt/luajit/armeabi
mkdir -p $DESTDIR
rm "$DESTDIR"/*.a
make clean
NDKABI=9
NDKVER=$NDK/toolchains/arm-linux-androideabi-4.9
NDKP=$NDKVER/prebuilt/${host_os}-x86_64/bin/arm-linux-androideabi-
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
make HOST_CC="gcc -m32" HOST_SYS="android" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF"
if [ -f $SRCDIR/src/libluajit.a ]; then
    mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a
fi;
# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)
NDKABI=14
NDKARCH="-march=armv7-a -mfloat-abi=softfp -Wl,--fix-cortex-a8"
DESTDIR=$APP_ANDROID_ROOT/prebuilt/luajit/armeabi-v7a
mkdir -p $DESTDIR
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
rm "$DESTDIR"/*.a
make clean
make HOST_CC="gcc -m32" HOST_SYS="android" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF $NDKARCH"

if [ -f $SRCDIR/src/libluajit.a ]; then
    mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a
fi;


# Android/x86, x86 (i686 SSE3), Android 4.0+ (ICS)
NDKABI=14
DESTDIR=$APP_ANDROID_ROOT/prebuilt/luajit/x86
NDKVER=$NDK/toolchains/x86-4.9
NDKP=$NDKVER/prebuilt/${host_os}-x86_64/bin/i686-linux-android-
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-x86"
mkdir -p $DESTDIR
rm "$DESTDIR"/*.a
make clean
make HOST_CC="gcc -m32" HOST_SYS="android" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF"

if [ -f $SRCDIR/src/libluajit.a ]; then
    mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a
fi;

cd $APP_ANDROID_ROOT
"$ANDROID_NDK_ROOT"/ndk-build $ANDROID_NDK_BUILD_FLAGS NDK_DEBUG=$NDK_DEBUG $NDK_BUILD_FLAGS -C "$APP_ANDROID_ROOT" $* \
"NDK_MODULE_PATH=${APP_ANDROID_ROOT}/prebuilt"
if [ ! -d ${DIR}/Plugins/Android ]; then
    mkdir -p ${DIR}/Plugins/Android
fi;
rm -rf ${DIR}/Plugins/Android/libs
cp -rf ${APP_ANDROID_ROOT}/libs ${DIR}/Plugins/Android/